//
// Description: 859. 亲密字符串
// Created by Loading on 2021/11/23.
//

#include <bits/stdc++.h>

using namespace std;

bool buddyStrings(string s, string goal) {
    if (s.size() != goal.size()) {
        return false;
    }
    if (s == goal) {
        unordered_map<char, int> m;
        for (char &c : s) {
            m[c]++;
            if (m[c] > 1) {
                return true;
            }
        }
        return false;
    } else {
        int first = -1, second = -1;
        for (int i = 0; i < s.size(); ++i) {
            if (s[i] != goal[i]) {
                if (first == -1) {
                    first = i;
                } else if (second == -1) {
                    second = i;
                } else {
                    return false;
                }
            }
        }
        return second != -1 && s[first] == goal[second] && s[second] == goal[first];
    }
}

int main() {
    string s = "ab";
    string goal = "ab";
    bool res = buddyStrings(s, goal);
    cout << res << endl;

    return 0;
}